<?php
class Custom_Model_Entities_ConfigEntity extends Custom_Model_EntityAbstract
{
	protected $_name = 'Config';
    protected $_primary = 'id';
    protected $_rowClass = 'Custom_Model_Entities_ConfigRow';
    
    /**
     * Get array config (key->value) with given group
     * 
     * @param string $group
     * @return null|Custom_Model_Entities_ConfigRow[]
     */
    public function getConfigGroup($group, $level = Custom_Constant::SINGLE)
    {
    	if (NULL == $group) {
            return NULL;
        }
        // get all value of config group
        $groupName = $group . "%";
        $select = $this->select();
        $select->where($this->quoteInto('name like ?', $groupName));
        $configs = $this->fetchAll($select);
        
        // convert to array(key->value)
        $configArr = array();
        foreach ($configs as $config) {
        	$name = str_replace($group . ".",	"", $config->name);        	
        	
        	if ($level == Custom_Constant::MULTI) {
        		$subNameArr = explode(".", $name, $level);
        		
        		$ptr = &$configArr;
	            foreach($subNameArr as $subName) $ptr = &$ptr[$subName];
	            $ptr = $config->value;      		
        	} else {
        		$configArr[$name] = $config->value;
        	}        	        	
        }    
        return $configArr;
    }        
}